package com.example.asus.diary_demo;


import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;
import org.litepal.crud.DataSupport;
import java.util.Date;

public class DiaryEditor extends AppCompatActivity {
    private int diaryID;
    private int status;
    private EditText mTitleText;
    private EditText mBodyText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diary_edit);
        setTheme(android.R.style.Theme_Black);
        setContentView(R.layout.diary_edit);
        mTitleText=(EditText)findViewById(R.id.title1);
        mBodyText=(EditText)findViewById(R.id.body);

        Intent intent=getIntent();
        status=intent.getIntExtra("Status",0);

        if(status==LifeDiary.EDIT){
            Diary diary=(Diary) intent.getSerializableExtra("Content");
            mTitleText.setText(diary.getTitle());
            mBodyText.setText(diary.getBody());
            diaryID=diary.getId();
            setTitle("编辑日记");
        }else
        {
            setTitle("创建日记");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.action_bar2,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.MENU_ITEM_SAVE:
                if(status==LifeDiary.INSERT){ //创建状态
                    if(!TextUtils.isEmpty(mTitleText.getText())) { //标题栏不为空
                        insertDiary();
                    }
                    //标题栏为空，自动添加标题
                    else if(TextUtils.isEmpty(mTitleText.getText())&&!TextUtils.isEmpty(mBodyText.getText())){
                        insertDiary_noTitle();
                    }
                }
                else if(status==LifeDiary.EDIT){ //编辑状态
                    if(!TextUtils.isEmpty(mTitleText.getText())) { //标题栏不为空
                        updateDiary();
                    }
                    //标题栏为空，自动添加标题
                    else if(TextUtils.isEmpty(mTitleText.getText())&&!TextUtils.isEmpty(mBodyText.getText())){
                        updateDiary_noTitle();
                    }
                    else{
                        deleteDiary();
                    }
                }
                Intent intent=new Intent(DiaryEditor.this,LifeDiary.class);
                startActivity(intent);
                finish();
                break;
            case R.id.MENU_ITEM_DELETE:
                deleteDiary();
                Toast.makeText(DiaryEditor.this, R.string.diary_delete_success,
                        Toast.LENGTH_LONG).show();
                Intent intent1=new Intent(DiaryEditor.this,LifeDiary.class);
                startActivity(intent1);
                finish();
                break;
            case R.id.MENU_ITEM_SHARE:
                Intent intent2=new Intent(Intent.ACTION_SEND);
                intent2.putExtra(Intent.EXTRA_TEXT,mTitleText.getText().toString()+" "+mBodyText.getText().toString());
                intent2.setType("text/plain");
                startActivity(Intent.createChooser(intent2,"分享到..."));
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void insertDiary(){
        Diary diary=new Diary();
        diary.setTitle(mTitleText.getText().toString());
        diary.setBody(mBodyText.getText().toString());
        diary.setDate(new Date());
        diary.saveFast();
    }

    private void insertDiary_noTitle(){
        Diary diary=new Diary();
        diary.setTitle("未命名");
        diary.setBody(mBodyText.getText().toString());
        diary.setDate(new Date());
        diary.saveFast();
    }

    private void updateDiary(){
        Diary diary=new Diary();
        diary.setTitle(mTitleText.getText().toString());
        diary.setBody(mBodyText.getText().toString());
        diary.setDate(new Date());
        diary.update(diaryID);
    }

    private void updateDiary_noTitle(){
        Diary diary=new Diary();
        diary.setTitle("未命名");
        diary.setBody(mBodyText.getText().toString());
        diary.setDate(new Date());
        diary.update(diaryID);
    }

    private void deleteDiary(){
        DataSupport.deleteAll(Diary.class,"id=?",String.valueOf(diaryID));
    }
}

